var app = getApp();
var SERVER_URL=app.globalData.SERVER_URL;
Page({
    onShow:function(){
        console.log(this.data);
    },
    data:{
        info:""
    },
    onLoad:function(options){
        let id = parseInt(options.id);
        this.getType(id);
        this.getAccountType();
    },
    getType:function(){
        let url = "/project/getProjectList";
        let openId = wx.getStorageSync('openId');
        url = SERVER_URL+url;
        var obj=[];
        var _self=this;
        wx.request({
            url: url,
            data: {
                "key": app.globalData.key,
                openId: openId,
            },
            method: 'GET',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success:function(res){
                let result = res.data;
                if(result && result.code==0){
                    _self.setData({
                        arrProject:result.data,
                        projectNum:result.data[0].ProjectNum,
                        projectName:result.data[0].ProjectName
                    });
                }
            }
        });
    },
    getAccountType:function(){
        let url = "/account/getAccountType";
        let openId = wx.getStorageSync('openId');
        url = SERVER_URL+url;
        var obj=[];
        var _self=this;
        wx.request({
            url: url,
            data: {
                "key": app.globalData.key,
                openId: openId,
            },
            method: 'GET',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success:function(res){
                let result = res.data;
                if(result && result.code==0){
                    _self.setData({
                        arrAccount:result.data,
                        accountCode:result.data[0].Code,
                        accountName:result.data[0].Name
                    });
                }
            }
        });
    },
    method_change:function(e){
        var index = e.detail.value;
        this.setData({
            projectNum:this.data.arrProject[index].ProjectNum,
            projectName:this.data.arrProject[index].ProjectName,
        });
        console.log(this.data);
    },
    type_change:function(e){
        var index = e.detail.value;
        this.setData({
            accountCode:this.data.arrAccount[index].Code,
            accountName:this.data.arrAccount[index].Name,
        });
        console.log(this.data);
    },
    listClick:function(e){
        let index = e.currentTarget.dataset.index;
        index =parseInt(index);
        var url="";

        wx.navigateTo({
            url: url
        });
    },
    text_change:function(e){
        var index = e.currentTarget.dataset.text;
        index = parseInt(index);
        var value = e.detail.value;
        switch (index){
            //金额
            case 1:
                this.setData({
                    money:value
                });
                break;
            //单据数
            case 2:
                this.setData({
                    num:value
                });
                break;
            //说明
            case 3:
                this.setData({
                    info:value
                });
                break;
        }
    },
    form_submit:function(){
        var data = this.data;
        var projectNum = data.projectNum ||"";
        var projectName = data.projectName ||"";
        var accountCode = data.accountCode ||"";
        var remaks = data.info ||"";
        var money = data.money ||0;
        var num = data.num ||0;

        if(money<=0){
            wx.showModal({
                title: '失败',
                content: '金额不能为空',
                showCancel:false,
                success: res=>{
                    if (res.confirm) {

                    }
                }
            });
            return;
        }
        if(num<=0){
            wx.showModal({
                title: '失败',
                content: '请输入票据张数',
                showCancel:false,
                success: res=>{
                    if (res.confirm) {

                    }
                }
            });
            return;
        }

        //submit
        var openId = wx.getStorageSync('openId');
        var url="/account/addAccount";
        url = SERVER_URL+url;
        wx.request({
            url: url,
            data: {
                "key": app.globalData.key,
                "OpenId": openId,
                "ProjectNum":projectNum,
                "ProjectName":projectName,
                "ReimburseType":accountCode,
                "ReimburseMoney":money,
                "DocumentsCount":num,
                "ReimburseExplain":remaks,
            },
            method: 'POST',
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success:function(res){
                let result = res.data;
                console.log(result);
                if(result && result.code==0){
                    app.globalData.data.info="";
                    wx.showModal({
                        title: '成功',
                        content: '已成功提交申请',
                        showCancel:false,
                        success: res=>{
                            if (res.confirm) {
                                wx.navigateTo({
                                    url: '../index'
                                });
                            }
                        }
                    });
                }
                else{
                    wx.showModal({
                        title: '失败',
                        content: result.msg,
                        showCancel:false,
                        success: res=>{
                            if (res.confirm) {

                            }
                        }
                    });
                }
            }
        })
    },
});